package com.cn.appcore.utils

import android.text.TextUtils
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 项目：sociallive
 * 包名：com.cn.sociallive.utils
 * 类名：TimeUtils
 * 创建时间：2022/5/26 2:48 下午
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
object TimeUtils {
    //十位时间戳字符串转小时分钟秒
    fun Hourmin(time: String): String? {
        val sdr = SimpleDateFormat("HH:mm:ss")
        val lcc = java.lang.Long.valueOf(time)
        val i = time.toInt()
        return sdr.format(Date(i * 1000L))
    }

    //十位时间戳字符串转年月
    fun YearMon(time: String): String? {
        val sdr = SimpleDateFormat("yyyy年MM月")
        val lcc = java.lang.Long.valueOf(time)
        val i = time.toInt()
        return sdr.format(Date(i * 1000L))
    }

    //十位时间戳字符串转月日
    fun MonthDay(time: String): String? {
        val sdr = SimpleDateFormat("MM月dd日")
        val lcc = java.lang.Long.valueOf(time)
        val i = time.toInt()
        return sdr.format(Date(i * 1000L))
    }

    //十位时间戳字符串转月日
    fun MonthEnglishDay(time: Int): String? {
        val sdr = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val lcc = time.toLong()
        return sdr.format(Date(time * 1000L))
    }

    //十位时间戳字符串转月日
    fun MonthEnglishDay(time: String): String? {
        val sdr = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val lcc = java.lang.Long.valueOf(time)
        val i = time.toInt()
        return sdr.format(Date(i * 1000L))
    }

    //十位时间戳字符串转月日
    fun MonthEnglish(time: Int): String? {
        val sdr = SimpleDateFormat("yyyy-MM-dd")
        val lcc = time.toLong()
        return sdr.format(Date(time * 1000L))
    }

    //获取10位字符串格式的时间戳
    fun getTime10(): String? {
        val time = System.currentTimeMillis() / 1000 //获取系统时间的10位的时间戳
        return time.toString()
    }


    //获取13位字符串格式的时间戳
    fun getTime(): Long {
        return System.currentTimeMillis()
    }

    fun YMDHMS(time: String): String? {
        val sdr = SimpleDateFormat("yyyy年MM月dd日HH:mm:ss")
        val lcc = java.lang.Long.valueOf(time)
        val i = time.toInt()
        return sdr.format(Date(i * 1000L))
    }


    //返回当前年月
    fun getNainYueDate(): String? {
        val date = Date()
        return SimpleDateFormat("yyyy-MM").format(date)
    }
    //返回当前年月日
    fun getNowDate(): String? {
        val date = Date()
        return SimpleDateFormat("yyyy-MM-dd").format(date)
    }
//    //返回当前年月日
//    fun getNowDate(): String? {
//        val date = Date()
//        return SimpleDateFormat("yyyy-MM-dd HH:mm").format(date)
//    }

    //返回当前年份
    fun getYear(): Int {
        val date = Date()
        val year = SimpleDateFormat("yyyy").format(date)
        return year.toInt()
    }

    //返回当前月份
    fun getMonth(): Int {
        val date = Date()
        val month = SimpleDateFormat("MM").format(date)
        return month.toInt()
    }

    //判断闰年
    fun isLeap(year: Int): Boolean {
        return if (year % 100 == 0 && year % 400 == 0 || year % 100 != 0 && year % 4 == 0) true else false
    }



    //时间戳转日期
    fun getTimeToStr(time : String): String {
        if(TextUtils.isEmpty(time)){
            return  ""
        }
        var sdf =  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format( Date(time.toLong()))
    }



    //获取制定日期中是星期几
    //返回星期几
    fun getDayofWeek(dateTime: String): Int {
        val cal = Calendar.getInstance()
        if (dateTime == "") {
            cal.time = Date(System.currentTimeMillis())
        } else {
            val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
            var date: Date?
            try {
                date = sdf.parse(dateTime)
            } catch (e: ParseException) {
                date = null
                e.printStackTrace()
            }
            if (date != null) {
                cal.time = Date(date.time)
            }
        }
        return cal[Calendar.DAY_OF_WEEK]
    }

    //返回当月天数
    fun getDays(year: Int, month: Int): Int {
        val days: Int
        var FebDay = 28
        if (isLeap(year)) FebDay = 29
        days = when (month) {
            1, 3, 5, 7, 8, 10, 12 -> 31
            4, 6, 9, 11 -> 30
            2 -> FebDay
            else -> 0
        }
        return days
    }

    //返回当月星期天数
    fun getSundays(year: Int, month: Int): Int {
        var sundays = 0
        val sdf = SimpleDateFormat("EEEE")
        val setDate = Calendar.getInstance()
        //从第一天开始
        var day: Int
        day = 1
        while (day <= getDays(year, month)) {
            setDate[Calendar.DATE] = day
            val str = sdf.format(setDate.time)
            if (str == "星期日") {
                sundays++
            }
            day++
        }
        return sundays
    }

    /**
     * 调此方法输入所要转换的时间输入例如（"2014-06-14-16-09-00"）返回时间戳
     *
     * @param time
     * @return 2023-03-07 15:35:46
     */
    fun dataOne(time: String?): String? {
        val sdr = SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss",
            Locale.CHINA
        )
        val date: Date
        var times: String? = null
        try {
            date = sdr.parse(time)
            val l = date.time
            val stf = l.toString()
            times = stf.substring(0, 13)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return times
    }
    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     *
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br></br>false
     */
    fun isDateOneBigger(str1: String?, str2: String?): Boolean {
        var isBigger = false
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        var dt1: Date? = null
        var dt2: Date? = null
        try {
            dt1 = sdf.parse(str1)
            dt2 = sdf.parse(str2)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        if (dt1!!.time > dt2!!.time) {
            isBigger = true
        } else if (dt1.time < dt2.time) {
            isBigger = false
        }
        return isBigger
    }

    /**
     * 获取当前时间。我的二维码生成。，
     */
    fun createCodeFileName(): String? {
        var fileName = ""
        val date = Date(System.currentTimeMillis())
        val dateFormat = SimpleDateFormat(
            "yyyy-MM-dd HH:mm"
        )
        fileName = dateFormat.format(date)
        return fileName
    }

    fun getCurrentTimeStamp(): String? {
        return (System.currentTimeMillis() / 1000).toString()
    }

    /**
     *  //yyyy-MM-dd'T'HH:mm:ss.SSSXXX
     *  转正常时间格式
     *  时间戳
     */
    fun dateUTCToStr(time: String?): Long {
        val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
        df.timeZone = TimeZone.getTimeZone("UTC")
        val output = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        var d: Date? = null
        try {
            d = df.parse(time)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return d!!.time
    }


    /**
     *秒转为时分
     */
    fun transFom(time:Int): String {
        var hh:Int=time/3600
        var mm:Int=(time%3600) /60
        var ss:Int=(time%3600) %60

        var hour = if(hh<10) "0$hh" else hh
        var minute =if(mm<10)"0$mm" else mm
        var second =if(ss<10)"0$ss" else ss

        return "$hour:$minute:$second"
    }

    /**
     * 时间选择器
     *
     * @param date
     * @return
     */
    fun getTime(date: Date?): String? { //可根据需要自行截取数据显示
        val format = SimpleDateFormat("yyyy-MM-dd")
        //        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date)
    }

    /**
     * 天数
     */
    fun getCurrentDays(timeDistance: Long): Int {
        return (timeDistance / (24 * 60 * 60 * 1000)).toInt()
    }
    /**
     * 小时
     */
    fun getCurrentHour(timeDistance: Long): Int {
        return (timeDistance % (24 * 60 * 60 * 1000)).toInt() / (60 * 60 * 1000)
    }
    /**
     * 分钟
     */
    fun getCurrentMinute(timeDistance: Long): Int {
        var minute = 0
        minute = (timeDistance % (24 * 60 * 60 * 1000)).toInt() % (60 * 60 * 1000) / (60 * 1000)
        return minute
    }
    /**
     * 秒
     */
    fun getCurrentMills(timeDistance: Long): Int {
        return (timeDistance % (24 * 60 * 60 * 1000)).toInt() % (60 * 60 * 1000) % (60 * 1000) / 1000
    }
}